﻿using HCQ2_Common.Constant;
using HCQ2_Model;
using HCQ2_Model.DocModel;
using HCQ2_Model.DocModel.APIParam;
using HCQ2_Model.ViewModel;
using HCQ2_Model.ViewModel.WebAPI;
using HCQ2WebAPI_Logic.BaseAPIController;
using HCQ2WebAPI_Logic.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2WebAPI_Logic.DocumentController
{
    /// <summary>
    /// 订单相关API
    /// </summary>
    public class OrderAPIController: BaseAPI
    {
        /// <summary>
        /// 获取【未付款订单】
        /// </summary>
        /// <returns></returns>
        public WebApiResultJsonModel ListPlaceAnOrder(OrderListParam param)
        {
            T_User user = getUser(param.userid);

            TableModel tableData = operateContext.bllSession.T_VROrder.GetOrderList(new HCQ2_Model.ParamsModel.OrderListParams {
                page = param.page,
                rows = param.rows,
                orderCode = param.OrderCode,
                statu = 0,
                userId = user.user_id
            }); 

            return ok(GlobalConstant.数据获取成功.ToString(), tableData);
        }

        /// <summary>
        /// 获取【已撤销订单】
        /// </summary>
        /// <returns></returns>
        public WebApiResultJsonModel ListRevokeOrder(OrderListParam param)
        {
            T_User user = getUser(param.userid);

            TableModel tableData = operateContext.bllSession.T_VROrder.GetOrderList(new HCQ2_Model.ParamsModel.OrderListParams
            {
                page = param.page,
                rows = param.rows,
                orderCode = param.OrderCode,
                statu = 1,
                userId = user.user_id
            });

            return ok(GlobalConstant.数据获取成功.ToString(),tableData);
        }

        /// <summary>
        /// 获取【交易成功订单】
        /// </summary>
        /// <returns></returns>
        public WebApiResultJsonModel ListCompleteOrder(OrderListParam param)
        {
            T_User user = getUser(param.userid);

            TableModel tableData = operateContext.bllSession.T_VROrder.GetOrderList(new HCQ2_Model.ParamsModel.OrderListParams
            {
                page = param.page,
                rows = param.rows,
                orderCode = param.OrderCode,
                statu = 2,
                userId = user.user_id
            });

            return ok(GlobalConstant.数据获取成功.ToString(),tableData);
        }

        /// <summary>
        /// 取消订单-只有【未付款订单】才能取消
        /// </summary>
        /// <returns></returns>
        [ModelValid]
        public WebApiResultJsonModel RevokeOrder(RevokeOrderParam param)
        {
            T_User user = getUser(param.userid);

            //验证用户是否拥有此订单
            T_VROrder vrorder = operateContext.bllSession.T_VROrder.FindSingle(w=>w.order_code.Equals(param.OrderCode) && w.user_id==user.user_id);
            if (vrorder == null) {
                return error("未找到用户和订单编码匹配的订单记录",null);
            }

            //验证订单状态
            if (vrorder.status == 1) {
                return error("订单【已经撤销】不能取消",null);
            }
            if (vrorder.status == 2)
            {
                return error("订单【已经交易成功】不能取消", null);
            }
            if (vrorder.status == 3)
            {
                return error("订单【已经交易关闭】不能取消", null);
            }

            //设置订单为撤销状态
            vrorder.status = 1;
            vrorder.handle_time = DateTime.Now;
            operateContext.bllSession.T_VROrder.Modify(vrorder,w=>w.order_code.Equals(param.OrderCode), "status", "handle_time");

            return ok("取消订单成功",null);
        }
    }
}
